package com.xyh.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xyh.po.User;
import com.xyh.service.impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

/**
 * @Author:kyle
 * @Date:2020/12/16 - 10:53
 * @Project:spring_demo1
 * @Package:com.xyh.controller
 * @Version:1.0
 */
@Controller
@RequestMapping("/user")
public class HelloController2 {

    @Autowired
    private UserServiceImpl userService;

    @RequestMapping("/helloRequest")
    public String list(HttpServletRequest request){
        System.out.println("helloRequest");
        List<User> list = userService.list();
        list.stream().forEach(System.out::println);
        request.setAttribute("request",list);
        return "hello";
    }

    @RequestMapping("/helloModelMap")
    public String helloModel(ModelMap modelMap){
        System.out.println("hellModelMap");
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.like("username","张");
        List<User> list = userService.list(wrapper);

        list.stream().forEach(System.out::println);
        modelMap.addAttribute("modelMap",list);
        return "hello";
    }

    @RequestMapping("/helloModelAndView")
    public ModelAndView helloModelAndView(){
        System.out.println("helloModelAndView");
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.between("registration_time","2018-1-1","2018-5-31");
        List<User> list = userService.list(wrapper);

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("modelAndView",list);
        modelAndView.setViewName("hello");
        return modelAndView;
    }

    @RequestMapping("/helloModel")
    public void helloModel(HttpServletRequest request, HttpServletResponse response, HttpSession session) throws ServletException, IOException {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.in("id", new Integer[]{1,2,3});
        List<User> list = userService.list(wrapper);
        list.stream().forEach(System.out::println);

        request.setAttribute("request",list);

        session.setAttribute("session",list);
        request.getRequestDispatcher("hello").forward(request,response);

    }
}
